﻿using Domain.Config;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Domain.Types;
using Infrastructure.Job;
using Infrastructure.UniOfWorkBase;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;
using static Utility.PolicyUtil;

namespace Application.Job
{
    /// <summary>
    ///  每月初统计用户奖励排行
    /// </summary>
    [Job("0 0 2 1 1/1 ? ")]
    public class UserRewardListJob : AbstractJob
    {
        public IUserRepository userRepository { get; set; }
        public ITaskTakeRepository taskTakeRepository { get; set; }
        public IRewardListRepository rewardListRepository { get; set; }
        public IInviteLogRepository inviteLogRepository { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public ILogger<UserLevelOutTimeJob> Logger { get; set; }
        public override void Run()
        {
            var month = DateTime.Today.AddMonths(-1);
            var start = new DateTime(month.Year, month.Month, 1);
            var end = new DateTime(month.Year, month.Month+1, 1);

            //上个月完成榜
            var list = taskTakeRepository.Entities.Where(i => i.CT >= start && i.CT < end && i.Status == TaskTakeAuditStatus.审核通过)
                .GroupBy(i=>i.UserID).OrderByDescending(i=>i.Count()).Take(10).ToList();

            for (int i = 0; i < list.Count(); i++)
            {
                var item = list[i];
                var u = userRepository.GetByKey(item.Key);

                RewardListAggregate rewardListAggregate = new RewardListAggregate();

                rewardListAggregate.CT = rewardListAggregate.UT = DateTime.Now;
                rewardListAggregate.Level = i + 1;
                rewardListAggregate.Money = SystemConfig.GetConfigByTime<int>("系统配置", $"奖励排行.{rewardListAggregate.Level}", TimeSpan.FromDays(1));
                rewardListAggregate.Month = start;
                rewardListAggregate.RewardListType = RewardListType.排行榜;
                rewardListAggregate.TaskCount = item.Count();
                rewardListAggregate.UserID = item.Key;
                rewardListAggregate.UserName = u.NickName??"未设置昵称";
                rewardListAggregate.UserPic = u.HeadImg;

                rewardListRepository.Insert(rewardListAggregate);
            }

            efUnitOfWork.Commit();
            efUnitOfWork.Reset();

            var list1= inviteLogRepository.Entities.Where(i => i.CT >= start && i.CT < end)
                .GroupBy(i => i.SuperorID).OrderByDescending(i => i.Count()).Take(10).ToList();

            for (int i = 0; i < list1.Count(); i++)
            {
                var item = list[i];
                var u = userRepository.GetByKey(item.Key);

                RewardListAggregate rewardListAggregate = new RewardListAggregate();

                rewardListAggregate.CT = rewardListAggregate.UT = DateTime.Now;
                rewardListAggregate.Level = i + 1;
                rewardListAggregate.Money = SystemConfig.GetConfigByTime<int>("系统配置", $"推荐排行.{rewardListAggregate.Level}", TimeSpan.FromDays(1));
                rewardListAggregate.Month = start;
                rewardListAggregate.RewardListType = RewardListType.推荐榜;
                rewardListAggregate.TaskCount = item.Count();
                rewardListAggregate.UserID = item.Key;
                rewardListAggregate.UserName = u.NickName ?? "未设置昵称";
                rewardListAggregate.UserPic = u.HeadImg;

                rewardListRepository.Insert(rewardListAggregate);
            }

            efUnitOfWork.Commit();

        }

    }
}
